package com.harvest.scientific.ladder.util

import android.annotation.SuppressLint
import com.harvest.scientific.ladder.constant.MemoryConstants


class ConvertUtils {

    companion object {

        @SuppressLint("DefaultLocale")
        @JvmStatic
        fun byte2FitMemorySize(byteSize: Long): String? {
            return byte2FitMemorySize(byteSize, 3)
        }

        /**
         * Size of byte to fit size of memory.
         *
         * to three decimal places
         *
         * @param byteSize  Size of byte.
         * @param precision The precision
         * @return fit size of memory
         */
        @JvmStatic
        @SuppressLint("DefaultLocale")
        fun byte2FitMemorySize(byteSize: Long, precision: Int): String? {
            require(!(precision < 0)) { "precision shouldn't be less than zero!" }
            return if (byteSize < 0) {
                throw IllegalArgumentException("byteSize shouldn't be less than zero!")
            } else if (byteSize < MemoryConstants.KB) {
                String.format("%." + precision + "fB", byteSize.toDouble())
            } else if (byteSize < MemoryConstants.MB) {
                java.lang.String.format(
                    "%." + precision + "fKB",
                    byteSize.toDouble() / MemoryConstants.KB
                )
            } else if (byteSize < MemoryConstants.GB) {
                java.lang.String.format(
                    "%." + precision + "fMB",
                    byteSize.toDouble() / MemoryConstants.MB
                )
            } else {
                java.lang.String.format(
                    "%." + precision + "fGB",
                    byteSize.toDouble() / MemoryConstants.GB
                )
            }
        }
    }
}